import {
  listColumns,
  rules
} from './index.const';
import {
  fileDownload
} from '@const/utils.js';
import {
  $loadFileSimply
} from '@const/loadFileOps.js';
import {
  selectInvoiceList,
  selectSaleTargetName,
  selectCompanyData,
  batchDownload,
  downloadInvoiceFile
} from '@api/guoke_deepexi_dms_order_center_v1.js';

import ElEditable from '@components/el-editable';

export default {
  components: {
    ElEditable,
  },
  data() {
    return {
      height_collapse: false,
      collapse: true,
      // 多条件查询订单
      tableQuery: {
        invoiceDateStart: '',
        invoiceDateEnd: '',
        saleDateStart: '',
        saleDateEnd: '',
        originalaFctoryId: '',
        newspaperCode: '',
        saleTargetCode: '',
        invoiceNum: '',
        orderStatus: '',
        uploadTimeStart: '',
        uploadTimeEnd: ''
      },
      invoiceDate: ['', ''],
      saleDate: ['', ''],
      uploadTime: ['', ''],
      dialogTableQuery: {
        saleDateStart: '',
        saleDateEnd: ''
      },
      pickerOptions1: {
        disabledDate: time => {
          if (this.dialogTableQuery.saleDateEnd !== '') {
            let one = 31 * 24 * 3600 * 1000;
            let saleDateEnd = new Date(this.dialogTableQuery.saleDateEnd + " 00:00:00").getTime()
            let maxTime = saleDateEnd - one
            return time.getTime() > saleDateEnd || time.getTime() < maxTime
          } else {
            return time.getTime() > Date.now()
          }
        }
      },
      pickerOptions2: {
        disabledDate: time => {
          if (this.dialogTableQuery.saleDateStart !== '') {
            let one = 31 * 24 * 3600 * 1000;
            let saleDateStart = new Date(this.dialogTableQuery.saleDateStart + " 00:00:00").getTime()
            let maxTime = saleDateStart + one
            if (maxTime > Date.now()) {
              return time.getTime() < saleDateStart || time.getTime() > Date.now()
            } else {
              return time.getTime() < saleDateStart || time.getTime() > maxTime
            }
          } else {
            return time.getTime() > Date.now()
          }
        }
      },
      rules,
      tableLoading: false,
      listColumns: listColumns, //表头
      delDialogVisible: false,
      //列表数据
      payload: {},
      loading: false,
      //经销商下拉
      companyOptions: [],
      //医院下拉
      saleTargetOptions: [],
      // 主列表分页
      currentPage: 1,
      pageSize: 20,
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    if (this.$refs.queryForm.$el.clientHeight > 90) {
      this.height_collapse = true
      this.collapse = false
    }
  },
  methods: {
    // 重置
    queryFormReset() {
      this.tableQuery = {
        invoiceDateStart: '',
        invoiceDateEnd: '',
        saleDateStart: '',
        saleDateEnd: '',
        companyId: '',
        newspaperCode: '',
        saleTargetCode: '',
        invoiceNum: '',
        orderStatus: '',
        uploadTimeStart: '',
        uploadTimeEnd: ''
      };
      this.invoiceDate = ['', '']
      this.saleDate = ['', '']
      this.uploadTime = ['', '']
      this.currentPage = 1;
      this.getList();
    },
    // 查询
    queryFormQuery() {
      this.currentPage = 1;
      this.getList();
    },
    //经销商下拉
    companyRemoteMethod(val) {
      if (val !== '') {
        this.loading = true;
        let query = {
          companyName: val,
        };
        this.$axios.$post(selectCompanyData, query).then(res => {
          this.companyOptions = res.payload;
          this.loading = false;
        });
      }
    },
    // 医院下拉
    saleTargetRemoteMethod(val) {
      if (val !== '') {
        this.loading = true;
        let query = {
          saleTargetName: val,
        };
        this.$axios.$post(selectSaleTargetName, query).then(res => {
          this.saleTargetOptions = res.payload;
          this.loading = false;
        });
      }
    },
    // 主列表数据
    getList() {
      this.tableLoading = true;
      const query = {
        ...this.tableQuery,
        page: this.currentPage,
        pageSize: this.pageSize,
        invoiceDateStart: this.invoiceDate ? this.invoiceDate[0] : '',
        invoiceDateEnd: this.invoiceDate ? this.invoiceDate[1] : '',
        saleDateStart: this.saleDate ? this.saleDate[0] : '',
        saleDateEnd: this.saleDate ? this.saleDate[1] : '',
        uploadTimeStart: this.uploadTime ? this.uploadTime[0] : '',
        uploadTimeEnd: this.uploadTime ? this.uploadTime[1] : ''
      };
      this.$axios.$post(selectInvoiceList, query).then(res => {
        if (res.code === '0') {
          this.payload = res.payload;
        }
        this.tableLoading = false;
      });
    },
    // 主列表分页器
    reloadProductData(val) {
      this.currentPage = val.number
      this.pageSize = val.size
      this.getList();
    },
    // /操作栏-下载
    download(scope) {
      $loadFileSimply({
          method: 'post',
          url: downloadInvoiceFile,
          timeout: '0',
          data: {
            orderInvoiceId: scope.row.orderInvoiceId
          }
        }).then(res => {
          if (res.byteLength) {
            fileDownload(res, `${scope.row.invoiceNum ? scope.row.invoiceNum : scope.row.fileName.substring(0,scope.row.fileName.lastIndexOf("."))}.${scope.row.url.replace(/.+\./, '').toLowerCase()}`)
          } else {
            this.$message({
              message: '下载失败',
              type: 'warning'
            })
          }
        })
        .catch(err => {
          this.$message({
            message: '下载失败',
            type: 'warning'
          })
        })
    },
    // 点击开启批量下载弹窗
    exportDialog() {
      this.delDialogVisible = true;
    },
    // 批量下载弹窗点击确认
    rushRedOk() {
      this.$refs.dialogQueryForm.validate((valid) => {
        if (valid) {
          $loadFileSimply({
            method: 'post',
            url: batchDownload,
            timeout: '0',
            data: {
              ...this.dialogTableQuery
            }
          }).then(res => {
            if (res.byteLength) {
              fileDownload(res, '发票文件.zip')
            } else {
              this.$message({
                message: '您选择的时间段，暂无数据可下载',
                type: 'warning'
              })
            }
          })
          this.delDialogVisible = false;
          this.dialogTableQuery = {
            saleDateStart: '',
            saleDateEnd: ''
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
};